package service;

import common.Exception.Results;
import dto.req.AuthorCommentReqDTO;
import dto.resp.AuthorCommentRespDTO;
import dto.resp.GetAuthorCommentRespDTO;
import entity.AuthorDO;
import service.impl.AuthorServiceImpl;
import service.impl.CommentArticleServiceImpl;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class CommentArticleService implements CommentArticleServiceImpl {

    private final static String tableName = " comment ";
    private final static String articleField = " article_id ";
    private final static String authorField = " author_id ";

    private final static String contextField = " context ";

    private final static String commentDate = " comment_date ";


    @Override
    public boolean addComment(AuthorCommentReqDTO request) {
        //进行插入操作，可能会导致插入失败
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            connection = JDBCUtils.getConnection();
            //Statement statement = connection.createStatement();
            String sql = "INSERT INTO " + tableName + "("+ articleField + "," + authorField+ "," + contextField + "," + "comment_date" +  ")" +
                    " VALUES (?,?,?,?)";
            pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, request.getArticleId());
            pstmt.setInt(2, request.getAuthorId());
            pstmt.setString(3, request.getComment());
            pstmt.setDate(4, new Date(System.currentTimeMillis()));
            // 执行SQL语句
            pstmt.executeUpdate();
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }finally {
            JDBCUtils.closeResource(connection,pstmt);
        }
    }

    @Override
    public Results<ArrayList<AuthorCommentRespDTO>> showAllCommentByArticleId(Integer articleId) {
        Results<ArrayList<AuthorCommentRespDTO>> results = new Results();
        ArrayList<AuthorCommentRespDTO> commentRespDTOArrayList = new ArrayList<>();
        if(articleId == null) {
            return results.fail("文章Id不存在");
        }

        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "SELECT author_id,context,comment_date FROM " + tableName+ " WHERE article_id = ? " ;
            pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, articleId);
            // 执行SQL语句
            rs = pstmt.executeQuery();
            if(!rs.next()){
                //用户不存在
                return results.fail("查询的用户不存在");
            }
            //避免循环依赖问题
            AuthorService authorService = new AuthorService();
            do {
                AuthorCommentRespDTO commentRespDTO = new AuthorCommentRespDTO();
                int authorId = rs.getInt(1);
                Results<GetAuthorCommentRespDTO> authorResults = authorService.queryUserById(authorId);
                if(!authorResults.isSuccess()){
                    commentRespDTO.setName("未知用户");
                }else {
                    commentRespDTO.setName(authorResults.getData().getName());
                }
                commentRespDTO.setComment(rs.getString(2));
                commentRespDTO.setDate( rs.getDate(3));
                commentRespDTOArrayList.add(commentRespDTO);
            }while (rs.next());
            results.setData(commentRespDTOArrayList);
            return results.success("查询成功！");
        } catch (Exception e) {
            return results.fail("查询异常： " + e.getMessage());
        }finally {
            JDBCUtils.closeResource(connection,pstmt,rs);
        }
    }
}
